/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.Message;
import me.zhengjie.modules.biz.service.MessageService;
import me.zhengjie.modules.biz.service.dto.MessageQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-18
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "message管理")
@RequestMapping("/api/message")
public class MessageController {

    private final MessageService messageService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('message:list')")
    public void exportMessage(HttpServletResponse response, MessageQueryCriteria criteria) throws IOException {
        messageService.download(messageService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询message")
    @ApiOperation("查询message")
//    @PreAuthorize("@el.check('message:list')")
    public ResponseEntity<Object> queryMessage(MessageQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(messageService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增message")
    @ApiOperation("新增message")
    @AnonymousAccess
    public ResponseEntity<Object> createMessage(@Validated @RequestBody Message resources){
        return new ResponseEntity<>(messageService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改message")
    @ApiOperation("修改message")
//    @PreAuthorize("@el.check('message:edit')")
    public ResponseEntity<Object> updateMessage(@Validated @RequestBody Message resources){
        messageService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除message")
    @ApiOperation("删除message")
//    @PreAuthorize("@el.check('message:del')")
    public ResponseEntity<Object> deleteMessage(@RequestBody Integer[] ids) {
        messageService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/readMessages")
    @Log("查询已读message")
    @ApiOperation("查询已读message")
    @AnonymousAccess
    public ResponseEntity<Object> queryReadMessage(MessageQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(messageService.queryReadMessage(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/unReadMessages")
    @Log("查询未读message")
    @ApiOperation("查询未读message")
    @AnonymousAccess
    public ResponseEntity<Object> queryUnReadMessage(MessageQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(messageService.queryUnReadMessage(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping("/read")
    @Log("消息已读")
    @ApiOperation("消息已读")
    @AnonymousAccess
    public ResponseEntity<Object> read(@RequestParam Integer id){
        messageService.read(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
